package com.china.produce.controller;

import com.china.produce.annotation.DCCValue;
import com.china.produce.annotation.DCValue;
import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2024/12/22
 */
@RequestMapping("/dcc")
@RestController
public class DccController {

    @DCCValue(value = "switches:666")
    public String switches;

    @DCValue(value = "open,1")
    public String open;

    @Autowired
    private CuratorFramework zookeeperClients;

    @GetMapping("/testSwitch")
    public String testSwitch() {

        return switches;
    }
    @GetMapping("/testOpen")
    public String testOpen() {
        return open;
    }

    @PostMapping("/create")
    public void create() throws Exception {

        zookeeperClients.create().forPath("/big-market-dcc/config/test");
        zookeeperClients.create().forPath("/big-market-dcc/config/test2", "hello".getBytes(StandardCharsets.UTF_8));
    }

    @DeleteMapping("/delete")
    public void del() throws Exception {
        zookeeperClients.delete().forPath("/big-market-dcc/config/test");
        zookeeperClients.delete().forPath("/big-market-dcc/config/test2");
    }

    @GetMapping("/get")
    public String get() throws Exception {

        byte[] bytes = zookeeperClients.getData().forPath("/big-market-dcc/config/test2");

       return new String(bytes);
    }

    @PutMapping("/update")
    public void update() throws Exception {

        zookeeperClients.setData().forPath("/big-market-dcc/config/test2", "world".getBytes(StandardCharsets.UTF_8));
    }
}
